define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'withdrawal_order/index' + location.search,
                    add_url: 'withdrawal_order/add',
                    multi_url: 'withdrawal_order/multi',
                    import_url: 'withdrawal_order/import',
                    table: 'withdrawal_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'order_number', title: __('Order_number'), operate: 'LIKE' },
                        { field: 'company.name', title: __('Company.name'), operate: 'LIKE' },
                        { field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE' },
                        // { field: 'type.name', title: __('Type.name'), operate: 'LIKE' },
                        { field: 'withdrawal_channel_id', title: __('Withdrawal_channel_id') },
                        { field: 'clearing_form', title: __('Clearing_form') },
                        { field: 'withdrawal_money', title: __('Withdrawal_money'), operate: 'BETWEEN' },
                        { field: 'service_charge', title: __('Service_charge'), operate: 'BETWEEN' },
                        { field: 'tax_rate_money', title: '税金', operate: 'BETWEEN' },
                        { field: 'arrival_amount', title: __('Arrival_amount'), operate: 'BETWEEN' },
                        { field: 'status', title: __('Status'), searchList: { "1": __('Status 1'), "2": __('Status 2') }, formatter: Table.api.formatter.status },
                        { field: 'payment_status', title: __('Payment_status'), formatter: Controller.api.formatter.payment_status },
                        { field: 'audit_status_text', title: '审核状态', formatter: Controller.api.formatter.audit_status },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: '详情',
                                icon: 'fa fa-qrcode',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'withdrawal_order/detail'
                            }, {
                                name: 'ordervoid',
                                text: '作废',
                                icon: 'fa fa-qrcode',
                                classname: 'btn btn-info btn-xs btn-detail btn-ajax',
                                confirm: '确认要将该订单作废吗？',
                                url: 'withdrawal_order/ordervoid',
                                success: function (data, ret) {
                                    setTimeout(function(){
                                        table.bootstrapTable('refresh');
                                    }, 1000)
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 批量审核
            $(".btn-orderaudit").click(function () {
                var ids = JSON.stringify(Table.api.selectedids(table));
                Fast.api.ajax({
                    url: 'withdrawal_order/orderaudit',
                    data: { ids: ids },
                    loading: false
                }, function (data) {
                    table.bootstrapTable('refresh');
                }, function (edata) {

                })
            })
        },
        ordervoid: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        orderaudit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                audit_status: function (value, row, index) {
                    //这里我们直接使用row的数据
                    var color = "#FF5722;";
                    if (row.audit_status == 2) {
                        color = "#009688;";
                    }
                    return '<span style="color: ' + color + '">' + row.audit_status_text + '</span>';
                },
                payment_status: function (value, row, index) {
                    //这里我们直接使用row的数据
                    if (row.audit_status == 1) {
                        return '<span style="color: #FF5722;">' + row.audit_status_text + '</span>';
                    } else {
                        var colors = "#FFB800;";
                        if (row.payment_status == 2) {
                            colors = "#009688;";
                        } else if (row.payment_status == 3) {
                            colors = "#FF5722;";
                        }
                        return '<span style="color: ' + colors + '">' + row.payment_status_text + '</span>';
                    }
                },
            }
        }
    };
    return Controller;
});
